<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="'仓库' + title"
    width="870"
    @close="close"
  >
    <el-form
      ref="formRef"
      inline
      label-width="140px"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="仓库编码：" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="仓库名称：" prop="fullName">
        <el-input v-model="form.fullName" />
      </el-form-item>
      <!-- <el-form-item label="所属公司：" prop="companyId">
        <el-tree-select
          v-model="form.companyId"
          :check-strictly="true"
          :data="tree"
          node-key="CompanyId"
          :props="defaultProps"
          :render-after-expand="false"
          show-checkbox
        />
      </el-form-item> -->
      <el-form-item label="联系人：" prop="contacts">
        <el-input v-model="form.contacts" />
      </el-form-item>
      <el-form-item label="联系电话：" prop="cTelephone">
        <el-input v-model="form.cTelephone" />
      </el-form-item>
      <el-form-item label="负责人：" prop="principal">
        <el-input v-model="form.principal" />
      </el-form-item>
      <el-form-item label="负责人电话：" prop="pTelephone">
        <el-input v-model="form.pTelephone" />
      </el-form-item>
      <el-form-item label="仓库地址：" prop="address">
        <el-input v-model="form.address" />
      </el-form-item>
      <el-form-item label="详细地址：" prop="addressDetail">
        <el-input v-model="form.addressDetail" />
      </el-form-item>
      <el-form-item label="邮编：" prop="postCode">
        <el-input v-model="form.postCode" />
      </el-form-item>
      <!-- <el-form-item label="创建人：">
        <el-input v-model="form.createUserName" />
      </el-form-item> -->
      <el-form-item label="显示顺序：" prop="sortCode">
        <el-input v-model="form.sortCode" />
      </el-form-item>
      <el-form-item label="是否启用：">
        <el-switch
          v-model="form.enabled"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="描述：">
        <el-input
          v-model="form.remark"
          autosize
          placeholder="请输入描述"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  // import VabIconSelector from '@/plugins/VabIconSelector'
  import { doEdit, getWhinfo } from '@/api/store'
  export default defineComponent({
    name: 'StoreEdit',
    props: {
      tree: {
        type: Array,
        default: () => [],
      },
    },
    // components: { VabIconSelector },
    emits: ['fetchData'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        params: {},
        queryForm: {
          whid: 'string',
        },
        parentList: [],
        list: [],
        defaultProps: {
          children: 'ChildNodes',
          label: 'FullName',
        },
        form: {
          code: '',
          fullName: '',
          ownerCom: 'D2358994-99FB-433B-A084-5D8D9072540B',
          contacts: '',
          cTelephone: '',
          principal: '',
          pTelephone: '',
          address: '',
          addressDetail: '',
          postCode: '',
          sortCode: '',
          remark: '',
          meta: {},
        },
        rules: {
          code: [
            { required: true, trigger: 'blur', message: '请输入仓库编码' },
          ],
          fullName: [
            { required: true, trigger: 'blur', message: '请输入仓库名称' },
          ],
          // companyId: [
          //   { required: true, trigger: 'blur', message: '请选择所属' },
          // ],
          contacts: [
            { required: false, trigger: 'blur', message: '请输入联系人' },
          ],
          cTelephone: [
            { required: false, trigger: 'blur', message: '请输入联系电话' },
          ],
          principal: [
            { required: false, trigger: 'blur', message: '请输入负责人' },
          ],
          pTelephone: [
            { required: false, trigger: 'blur', message: '请输入负责人电话' },
          ],
          address: [
            { required: false, trigger: 'blur', message: '请输入仓库地址' },
          ],
          addressDetail: [
            { required: false, trigger: 'blur', message: '请输入详细地址' },
          ],
          postCode: [
            { required: false, trigger: 'blur', message: '请输入邮编' },
          ],
          enabled: [
            { required: false, trigger: 'blur', message: '请选择是否可用' },
          ],
          createUserName: [
            { required: false, trigger: 'blur', message: '请输入创建人' },
          ],
          sortCode: [
            { required: false, trigger: 'change', message: '请输入显示顺序' },
          ],
          remark: [
            { required: false, trigger: 'change', message: '请输入描述' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      })
      //获取详情数据
      const getwhinfo = async () => {
        state.listLoading = true
        const {
          data: { data },
        } = await getWhinfo(state.queryForm)
        state.form = data
        state.listLoading = false
      }
      //编辑传值
      const showEdit = (row, type) => {
        if (type != 'edit') {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.queryForm = { whid: row.WHID }
          getwhinfo()
        }
        state.dialogFormVisible = true
      }
      //弹框关闭
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          meta: {
            icon: '',
          },
        }
        state.dialogFormVisible = false
      }
      //数据保存
      const save = async () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { message } = await doEdit(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            close()
            emit('fetchData')
          }
        })
      }
      return {
        ...toRefs(state),
        showEdit,
        getwhinfo,
        close,
        save,
      }
    },
  })
</script>
<style scoped>
  .el-form {
    display: flex;
    flex-flow: wrap;
    justify-content: left;
  }
  .el-form >>> .el-input {
    width: 234px !important;
  }
  .el-form-item {
    width: 380px;
  }
</style>
